<template>
  <div class="root">
    <div class="moon"></div>
    <div class="mountain hidden-xs-only"></div>
    <div class="mountain two hidden-xs-only"></div>
    <div class="land"></div>
    <div class="house">
      <div class="roof1"></div>
      <div class="wall2">
        <div class="window2"></div>
        <div class="attic"></div>
      </div>
      <div class="wall1">
        <div class="window"></div>
      </div>
      <div class="roof2"></div>
    </div>
    <div class="login">
      <el-card class="box-card" shadow="always">
        <div class="card_header">
          <h1>Welcome to 鲤享</h1>
        </div>
        <el-row :gutter="20">
          <el-col :md="12" :lg="12" :xs="24" class="card_img hidden-md-and-u">
            <div
              class="house hidden-sm-and-up"
              style="position: relative; top: 30%; left: 20%"
            >
              <div class="roof1"></div>
              <div class="wall2">
                <div class="window2"></div>
                <div class="attic"></div>
              </div>
              <div class="wall1">
                <div class="window"></div>
              </div>
              <div class="roof2"></div>
            </div>
            <el-image
              class="hidden-xs-only"
              style="height: 100%; width: 100%"
              src="http://www.fengfengzhidao.com/media/article/img_55.jpg?id=fichugulqz"
            ></el-image>
          </el-col>
          <el-col :md="12" :lg="12" :xs="24" class="card-right">
            <el-form
              ref="ruleForm"
              :label-position="labelPosition"
              :model="loginForm"
              status-icon
              :rules="rules"
            >
              <el-form-item prop="username">
                <label>用户名</label>
                <el-input
                  v-model="loginForm.username"
                  type="text"
                  autocomplete="off"
                  placeholder="请输入用户名"
                ></el-input>
              </el-form-item>
              <el-form-item prop="password">
                <label>密码</label>
                <el-input
                  v-model="loginForm.password"
                  type="password"
                  autocomplete="off"
                  placeholder="请输入密码"
                ></el-input>
              </el-form-item>
              <div class="register_link">
                没有账号？前往 &nbsp;<router-link to="/register">注册</router-link>
              </div>
              <el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
              </el-form-item>
            </el-form>
          </el-col>
        </el-row>
      </el-card>
    </div>
  </div>
</template>

<script>
import { ElNotification } from "element-plus";
export default {
  data() {
    const checkUsername = (rule, value, callback) => {
      if (value === "") {
        return callback(new Error("用户名不能为空"));
      } else {
        if (this.loginForm.username !== "") {
          callback();
        }
        callback();
      }
    };

    const checkPassword = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("密码不能为空"));
      } else {
        if (this.loginForm.password !== "") {
          callback();
        }
        callback();
      }
    };
    return {
      labelPosition: "left",

      loginForm: {
        username: "",
        password: "",
        token: "",
      },
      rules: {
        username: [{ validator: checkUsername, trigger: "blur" }],
        password: [{ validator: checkPassword, trigger: "blur" }],
      },
    };
  },
  created() {
    // this.$http
    //   .get("http://127.0.0.1:8000/token")
    //   .then((response) => (this.loginForm.token = response));
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // this.$http
          //   .post("http://127.0.0.1:8000/login", {
          //     _token: this.loginForm.token,
          //     username: this.loginForm.username,
          //   })
          //   .then((response) => alert(response));

          ElNotification({
            title: "登录成功",
            showClose: false,
            message: "正在前往首页",
            type: "success",
            duration: 3000,
            position: "bottom-right",
          });

          setTimeout(() => {
            this.$router.push({ path: "/home" });
          }, 3000);
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
  setup() {
    return {};
  },
};
</script>

<style lang="scss" scoped>
@import url("../../assets/background.css");
.root {
  height: 100vh;
  margin: 0;
  padding: 0;
  background: linear-gradient(#021317, #293841, #80afa3, #ded8c9);
  position: relative;
  overflow: hidden;
}
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  @media screen and (min-width: 1100px) {
    padding-top: 14%;
  }
  @media screen and (max-width: 1100px) {
    padding-top: 10%;
  }

  .box-card {
    z-index: 20;
    background: #ffffff85;
    width: 60%;
    border-radius: 20px;
    .card_header {
      margin-bottom: 10px;
    }
    .card-right {
      .el-form {
        width: 100%;
        .register_link {
          margin: 10px;
          display: flex;
          justify-content: flex-end;
        }
      }
    }
    .card-right,
    .card_header {
      display: flex;
      justify-content: center;
      align-items: center;
    }
  }
}
</style>
